package com.example.sxmall.service.action.impl;

import com.example.sxmall.auth.Authentication;
import com.example.sxmall.auth.UserInfo;
import com.example.sxmall.controller.shopCart.GlobalConstant;
import com.example.sxmall.controller.shopCart.ProductEnums;
import com.example.sxmall.controller.shopCart.vo.ProductVO;
import com.example.sxmall.controller.shopCart.vo.ShopCartVO;
import com.example.sxmall.controller.shopCart.vo.StoreProductVO;
import com.example.sxmall.controller.shopCart.vo.StoreVO;
import com.example.sxmall.exception.AppException;
import com.example.sxmall.redis.ShopCartKey;
import com.example.sxmall.service.ShopCartService;
import com.example.sxmall.service.action.ActionCartService;
import com.example.sxmall.service.constant.ShopCartMessageConstant;
import com.example.sxmall.service.dto.OperateCartDTO;
import com.example.sxmall.service.dto.ShopCartDTO;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: TODO
 * @Author whh-yt
 * @Date 2022/3/17 4:18 下午
 */
@Service
public class AddCartActionService extends ActionCartService {

    @Override
    public ShopCartVO execute(OperateCartDTO operateCartDTO) {
        UserInfo userInfo = Authentication.getUserInfo();
        // 需要添加的商品
        Long storeId = operateCartDTO.getStoreId();
        ProductVO productVO = operateCartDTO.getProduct();
        if (Objects.isNull(productVO)) {
            throw new AppException(ShopCartMessageConstant.ADD_PRODUCT_ERROR);
        }
        MaterialEntity materialEntity = commonProductService.getMaterialBySku(productVO.getSkuCode());
        if (ProductEnums.ProductOnShelfEnum.OFF_SHELF.getType().equals(materialEntity.getOnShelf())) {
            throw new AppException(ShopCartMessageConstant.PRODUCT_OFF_SHELF);
        }
        // 当前用户购物车的商品
        ShopCartDTO shopCartCache = redisService.get(ShopCartKey.SHOP_CART_KEY, userInfo.getUnionId(), ShopCartDTO.class);
        // 新购物车缓存对象
        ShopCartDTO shopCartDTO = new ShopCartDTO();
        shopCartDTO.setVersion(ShopCartService.SHOPCART_VERSION);

        // 购物车为空
        if (Objects.isNull(shopCartCache) || CollectionUtils.isEmpty(shopCartCache.getStoreProductList())) {
            StoreProductVO storeProductVO = new StoreProductVO();
            storeProductVO.setStore(new StoreVO(GlobalConstant.WAHAHA_STORE_ID, GlobalConstant.WAHAHA_STORE));
            productVO.setOnShelf(ProductEnums.ProductOnShelfEnum.ON_SHELF.getType());
            productVO.setCheck(Boolean.TRUE);
            List<ProductVO> productList = new ArrayList<>();
            productList.add(productVO);
            storeProductVO.setProductList(productList);
            shopCartDTO.setStoreProductList(Collections.singletonList(storeProductVO));
        } else {
            List<StoreProductVO> storeProductListCache = shopCartCache.getStoreProductList();
            //TODO 目前只有一个店铺 所以简化一下 从购物车取出当前店铺和商品
            StoreProductVO storeProductCache = storeProductListCache.get(0);
            // 商铺信息
            StoreVO store = storeProductCache.getStore();
            // 商品信息
            List<ProductVO> productListCache = storeProductCache.getProductList();
            Map<String, ProductVO> cartSkuMap = productListCache.stream().collect(Collectors.toMap(ProductVO::getSkuCode, item -> item));
            String skuCode = productVO.getSkuCode();
            if (cartSkuMap.containsKey(skuCode)) {
                ProductVO cartProduct = cartSkuMap.get(skuCode);
                cartProduct.setCheck(Boolean.TRUE);
                cartProduct.setBuyCount(cartProduct.getBuyCount() + productVO.getBuyCount());
            } else {
                productVO.setCheck(Boolean.TRUE);
                productListCache.add(productVO);
            }
            storeProductCache.setProductList(productListCache);
            shopCartDTO.setStoreProductList(Collections.singletonList(storeProductCache));
        }
        redisService.set(ShopCartKey.SHOP_CART_KEY, userInfo.getUnionId(), shopCartDTO);
        return new ShopCartVO();
    }
}
